<?php

namespace App\Providers;

use App\Models\User;
use App\observers\UserObserver;
use HttpException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
//        $this->app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
//            $fractal = new Manager();
//            $fractal->setSerializer(new ArraySerializer());
//
//            return new Fractal($fractal, 'include', ',', false);
//        });

        app('Dingo\Api\Exception\Handler')->register(function (ModelNotFoundException $e) {
            throw new NotFoundHttpException('404 Not Found');
        });

        app('Dingo\Api\Exception\Handler')->register(function (ValidationException $e) {
            throw new BadRequestHttpException($e->validator->errors()->first());
        });

        app('Dingo\Api\Exception\Handler')->register(function (\PDOException $e) {
            throw new \Error(trans('errorCode.50000'));
        });

        app('Dingo\Api\Exception\Handler')->register(function (HttpException $e) {
            throw new \Error(trans('httpErrorCode.500'));
        });
    }

    public function boot()
    {
        User::observe(UserObserver::class);
    }
}
